package org.bigjava.user.web.controller;

import org.bigjava.bean.Types;
import org.bigjava.user.service.impl.TypesServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/classify/")
public class TypesFunctionController {
    @Autowired
    TypesServiceImpl service;


    //添加一级分类
    @RequestMapping("insert")
    public void insertPrimaryClassification(String typename) {
        service.insertPrimaryClassification(typename);
    }

    //添加二级分类
    @RequestMapping("insertTwoType")
    public void insertSecondaryClassification(String onetypename, String twotypename) {
        service.insertSecondaryClassification(onetypename, twotypename);
    }

    //修改分类信息
    @RequestMapping("updateType")
    public void updatePrimaryClassification(Types types) {
        service.updatePrimaryClassification(types);
    }

    //删除分类
    @RequestMapping("deleteType")
    public void deleteSecondaryClassification(String typename) {
        service.delteType(typename);
    }

    //查询分类(支持模糊查询,和条件查询）
    @RequestMapping("queryType")
    public void queryType(Types type , HttpSession session) {
        List<Types> types = service.queryType(type);
        List<String> querytype = types.stream().map(Types::getTypeName).collect(Collectors.toList());
        session.getServletContext().setAttribute("querytype",querytype);
    }


    //跳转到分类详情界面
    @RequestMapping("QueryTypeByName")
    public ModelAndView queryTypeByName(@RequestParam("typename") String typename, HttpSession session) {
        Types type = service.queryTypeByName(typename);

        session.getServletContext().setAttribute("TypeDetails",type);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("classDetails");
        return modelAndView;
    }
}
